using System;
using System.Collections.Generic;
using System.Text;
using Rapier.Kodachi.Shared;
using Microsoft.Win32;
using System.IO;
using System.Xml.Serialization;

namespace Rapier.Kodachi.WebSearch
{
    public class WebSearchPlugin : Plugin
    {
        #region Properties
        /*
         * This property is static so all my classes can get to it
         */
        private static WebSearchSettings _WebSearchSettings;
        public static WebSearchSettings WebSearchSettings
        {
            get { return _WebSearchSettings; }
        }
        #endregion

        #region Constructor
        public WebSearchPlugin(Host host) : base(host)
        {
            LoadSettings();
        }
        #endregion

        #region Overrides
        public override string Author
        {
            get { return "leeb"; }
        }

        public override string Url
        {
            get { return "http://www.rapiersoftware.com"; }
        }

        /*
         * Should we present a "Settings" option for the user to click for this plugin?
         */
        public override bool HasSettings
        {
            get { return true; }
        }

        /*
         * Show your settings dialog here.
         */
        public override bool ShowSettings()
        {
            Settings settings = new Settings(this);

            if (settings.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                LoadSettings();

            return true;
        }

        /*
         * This is where we handle saving the plugin's settings. The following is one way
         * to do it, but not a required way.
         * Note that any settings files should be saved in SharedObjects.UserDataPath or a subfolder of that.
         */
        public override void SaveSettings()
        {
            Host.DataManager.SaveSettings(this, WebSearchSettings);
        }

        
        public override void UpdateDatabase()
        {
            /*
             * This plugin creates one item for each search engine that is set up
             */
            foreach (WebSearchEngine searchEngine in WebSearchPlugin.WebSearchSettings.Engines)
            {
                WebSearchItem searchItem = new WebSearchItem(this, searchEngine.Name);
                searchItem.DefaultAction = "Search";
                searchItem.Properties["WebSearchEngine"] = searchEngine;
                searchItem.GetImage = delegate { return searchItem.GetSearchEngineImage(); };
                Host.AddItem(searchItem);
            }


            /*
             * This plugin creates one action for each search engine that is set up,
             *  plus an additional action called "SearchFor" that is used with the search engine items.
             *  (This allows for two ways of searching: [Text->Search with Engine] or [Engine->Search For->Text]
             */
            foreach (WebSearchEngine searchEngine in WebSearchSettings.Engines)
                Host.AddAction(new WebSearchAction(this, searchEngine));

            Host.AddAction(new SearchFor(this));
        }

        /*
         * We do not need to extend any items.
         * Extending is used to add properties to an existing item (usually an item created by another plugin)
         * An example of this is the iTunes plugin, which adds properties to the iTunes shortcut that
         *  already gets indexed by the CorePlugin.
         */
        public override void ExtendItem(Item item)
        {
        }
        #endregion

        #region Private Methods
        private void LoadSettings()
        {

            _WebSearchSettings = Host.DataManager.LoadSettings<WebSearchSettings>(this);
            if (_WebSearchSettings == null)
            {
                _WebSearchSettings = new WebSearchSettings(Host);

                WebSearchEngine google = new WebSearchEngine();
                google.Name = "Google";
                google.QueryFormat = "http://www.google.com/search?hl=en&q={0}";
                _WebSearchSettings.Engines.Add(google);

                WebSearchEngine yahoo = new WebSearchEngine();
                yahoo.Name = "Yahoo";
                yahoo.QueryFormat = "http://search.yahoo.com/search?p={0}";
                _WebSearchSettings.Engines.Add(yahoo);

                WebSearchEngine imdb = new WebSearchEngine();
                imdb.Name = "IMDB";
                imdb.QueryFormat = "http://imdb.com/find?s=all&q={0}";
                _WebSearchSettings.Engines.Add(imdb);
            }
        }
        #endregion
    }
}
